package com.anquing.securitydemo.filter.phonecode;

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author wangqq65
 * @since 2021/8/17 14:37
 */
@Component("phoneCodeAuthenticationConfigurer")
public class PhoneCodeAuthenticationConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    @Resource
    private UserDetailsService userDetailsService;


    @Override
    public void configure(HttpSecurity http) throws Exception {

        PhoneCodeAuthenticationFilter phoneCodeAuthenticationFilter = new PhoneCodeAuthenticationFilter();
        phoneCodeAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));

        PhoneCodeAuthenticationProvider phoneCodeAuthenticationProvider = new PhoneCodeAuthenticationProvider();
        phoneCodeAuthenticationProvider.setUserDetailsService(userDetailsService);

        http
                .authenticationProvider(phoneCodeAuthenticationProvider)
                .addFilterAfter(phoneCodeAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }
}
